package test;

import java.awt.EventQueue;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.border.LineBorder;
import java.awt.Color;

import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;
import javax.swing.ListSelectionModel;
import com.keffect.effects.MultipleEffect;
import com.keffect.effects.animatedEffects.Darken;
import com.keffect.effects.animatedEffects.DarkenReverse;
import com.keffect.effects.animatedEffects.FadeIn;
import com.keffect.effects.animatedEffects.Shadow;
import com.keffect.effects.animatedEffects.SplitHorizontal;
import com.keffect.effects.animatedEffects.SplitHorizontalFade;
import com.keffect.effects.animatedEffects.SplitVertical;
import com.keffect.effects.animatedEffects.SplitVerticalFade;
import com.keffect.effects.animatedEffects.EntryBoucing;
import com.keffect.effects.animatedEffects.Shape;
import com.keffect.effects.animatedEffects.ZoomIn;
import com.keffect.effects.staticEffects.BackgroundImage;
import com.keffect.effects.staticEffects.ShadowFrame;
import com.keffect.effects.staticEffects.StaticShadow;
import com.keffect.effects.staticEffects.Transparency;
import com.keffectpanel.KEffectPanel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;

public class gui_main {

	private JFrame frmTestProgram;
	private JPanel panel;
	private JButton btnNewButton;
	private JButton btnShadow;
	private JButton btnSplitHorizontal;
	private JButton btnSplitHorizontalFade;
	private JButton btnSplitVertical;
	private JButton btnSplitVerticalFade;
	private JPanel panel_1;
	private KEffectPanel effectPanel;
	private JLabel lblHelloWorld;
	private JLabel lblThisIsA;
	private JTextField textField;
	private JLabel lblEnterYourName;
	private JTextField textField_1;
	private JLabel lblEnterAMessage;
	@SuppressWarnings("rawtypes")
	private JList list;
	private JTable table;
	private JTextField txtDuration;
	private JLabel lblTransitionDuration;
	private JButton btnShadowFrame;
	private JButton btnTransparency;
	private JButton btnPickAColor;
	private JPanel panelColor;
	private JButton btnStaticShadow;
	private JButton btnDarken;
	private JButton btnOpositeDarken;
	private JButton btnBackgroundImage;
	private JPanel panel_2;
	private JRadioButton rdbtnNormal;
	private JRadioButton rdbtnStretch;
	private JRadioButton rdbtnFit;
	private JRadioButton rdbtnTile;
	private JPanel panel_3;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JButton btnMultipleEffects;
	private JButton btnEfecto;
	private JButton btnEfecto_1;
	private JButton btnEfecto_2;
	private JLabel lblMatiasVarela;
	private JLabel lblMtsvrlgmailcom;
	private JLabel label;


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			JFrame.setDefaultLookAndFeelDecorated(true);
			JDialog.setDefaultLookAndFeelDecorated(true);
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {

			public void run() {
				try {
					gui_main window = new gui_main();
					window.frmTestProgram.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}


	/**
	 * Create the application.
	 */
	public gui_main() {
		initialize();
	}


	/**
	 * Initialize the contents of the frame.
	 */
	@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
	private void initialize() {
		this.frmTestProgram = new JFrame();
		this.frmTestProgram.setTitle("Test program");
		this.frmTestProgram.setBounds(100, 100, 889, 721);
		this.frmTestProgram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.frmTestProgram.getContentPane().setLayout(null);

		this.panel = new JPanel();
		this.panel.setBorder(new LineBorder(new Color(0, 0, 0)));
		this.panel.setBounds(10, 11, 206, 661);
		this.frmTestProgram.getContentPane().add(this.panel);
		this.panel.setLayout(null);

		this.btnNewButton = new JButton("Fade In");
		this.btnNewButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				effectPanel.setEffect(new FadeIn(getDuration()));
				effectPanel.startEffect();
			}
		});
		this.btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 13));
		this.btnNewButton.setBounds(10, 11, 186, 32);
		this.panel.add(this.btnNewButton);

		this.btnShadow = new JButton("Shadow");
		this.btnShadow.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				effectPanel.setEffect(new Shadow(getDuration(),panelColor.getBackground()));
				effectPanel.startEffect();
				
			}
		});
		this.btnShadow.setFont(new Font("Tahoma", Font.BOLD, 13));
		this.btnShadow.setBounds(10, 54, 186, 32);
		this.panel.add(this.btnShadow);

		this.btnSplitHorizontal = new JButton("Split Horizontal");
		this.btnSplitHorizontal.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				effectPanel.setEffect(new SplitHorizontal(getDuration(),panelColor.getBackground()));
				effectPanel.startEffect();
			}
		});
		this.btnSplitHorizontal.setFont(new Font("Tahoma", Font.BOLD, 13));
		this.btnSplitHorizontal.setBounds(10, 97, 186, 32);
		this.panel.add(this.btnSplitHorizontal);

		this.btnSplitHorizontalFade = new JButton("Split Horizontal Fade");
		this.btnSplitHorizontalFade.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				effectPanel.setEffect(new SplitHorizontalFade(getDuration(),panelColor.getBackground()));
				effectPanel.startEffect();
			}
		});
		this.btnSplitHorizontalFade.setFont(new Font("Tahoma", Font.BOLD, 13));
		this.btnSplitHorizontalFade.setBounds(10, 140, 186, 32);
		this.panel.add(this.btnSplitHorizontalFade);

		this.btnSplitVertical = new JButton("Split Vertical");
		this.btnSplitVertical.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				effectPanel.setEffect(new SplitVertical(getDuration(),panelColor.getBackground()));
				effectPanel.startEffect();
			}
		});
		this.btnSplitVertical.setFont(new Font("Tahoma", Font.BOLD, 13));
		this.btnSplitVertical.setBounds(10, 183, 186, 32);
		this.panel.add(this.btnSplitVertical);

		this.btnSplitVerticalFade = new JButton("Split Vertical Fade");
		this.btnSplitVerticalFade.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				effectPanel.setEffect(new SplitVerticalFade(getDuration(),panelColor.getBackground()));
				effectPanel.startEffect();
			}
		});
		this.btnSplitVerticalFade.setFont(new Font("Tahoma", Font.BOLD, 13));
		this.btnSplitVerticalFade.setBounds(10, 226, 186, 32);
		this.panel.add(this.btnSplitVerticalFade);

		this.txtDuration = new JTextField();
		this.txtDuration.setText("1000");
		this.txtDuration.setBounds(10, 587, 186, 20);
		this.panel.add(this.txtDuration);
		this.txtDuration.setColumns(10);

		this.lblTransitionDuration = new JLabel("duration time (milliseconds)");
		this.lblTransitionDuration.setBounds(10, 562, 186, 14);
		this.panel.add(this.lblTransitionDuration);

		this.btnShadowFrame = new JButton("Shadow Frame");
		this.btnShadowFrame.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				String ans = JOptionPane.showInputDialog("Enter px property. px >= 0","20");
				if (ans != null) {
					try {
						int px = Integer.parseInt(ans);
						if (px > 0) {
							effectPanel.setEffect(new ShadowFrame(px,panelColor.getBackground()));
							effectPanel.startEffect();
							return;
						}

					} catch (NumberFormatException e) {
						e.printStackTrace();
					}

					JOptionPane.showMessageDialog(null, "px must be an integer. px >= 0", "Error input", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		this.btnShadowFrame.setFont(new Font("Tahoma", Font.BOLD, 13));
		this.btnShadowFrame.setBounds(10, 269, 186, 32);
		this.panel.add(this.btnShadowFrame);

		this.btnTransparency = new JButton("Transparency");
		this.btnTransparency.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				String ans = JOptionPane.showInputDialog("Enter alpha.\n 0.0 <= alpha <= 1.0)","0.5");
				if (ans != null) {
					try {
						float alpha = Float.parseFloat(ans);
						if (alpha >= 0.0 && alpha <= 1.0) {
							effectPanel.setEffect(new Transparency(alpha));
							effectPanel.startEffect();
							return;
						}

					} catch (NumberFormatException e) {
						e.printStackTrace();
					}

					JOptionPane.showMessageDialog(null, "Alpha must be a float number. 0.0 <= alpha <= 1.0", "Error input", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		this.btnTransparency.setFont(new Font("Tahoma", Font.BOLD, 13));
		this.btnTransparency.setBounds(10, 312, 186, 32);
		this.panel.add(this.btnTransparency);
		
		this.btnPickAColor = new JButton("pick a color");
		this.btnPickAColor.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Color color = JColorChooser.showDialog(null, "Select shadow color", Color.black);
				if (color != null){
					panelColor.setBackground(color);
				}
			}
		});
		this.btnPickAColor.setFont(new Font("Tahoma", Font.BOLD, 11));
		this.btnPickAColor.setBounds(10, 618, 113, 32);
		this.panel.add(this.btnPickAColor);
		
		this.panelColor = new JPanel();
		this.panelColor.setBackground(Color.BLACK);
		this.panelColor.setBorder(new LineBorder(new Color(0, 0, 0)));
		this.panelColor.setBounds(133, 618, 43, 32);
		this.panel.add(this.panelColor);
		
		this.btnStaticShadow = new JButton("Static Shadow");
		this.btnStaticShadow.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String ans = JOptionPane.showInputDialog("Enter alpha.\n 0.0 <= alpha <= 1.0)","0.5");
				if (ans != null) {
					try {
						float alpha = Float.parseFloat(ans);
						if (alpha >= 0.0 && alpha <= 1.0) {
							effectPanel.setEffect(new StaticShadow(alpha,panelColor.getBackground()));
							effectPanel.startEffect();
							return;
						}

					} catch (NumberFormatException e2) {
						e2.printStackTrace();
					}

					JOptionPane.showMessageDialog(null, "Alpha must be a float number. 0.0 <= alpha <= 1.0", "Error input", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		this.btnStaticShadow.setFont(new Font("Tahoma", Font.BOLD, 13));
		this.btnStaticShadow.setBounds(10, 355, 186, 32);
		this.panel.add(this.btnStaticShadow);
		
		this.btnDarken = new JButton("Darken");
		this.btnDarken.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String ans = JOptionPane.showInputDialog("Enter a dark percent","50");
				if (ans != null) {
					try {
						int percent = Integer.parseInt(ans);
						if (percent > 0) {
							effectPanel.setEffect(new Darken(getDuration(),percent,panelColor.getBackground()));
							effectPanel.startEffect();
							return;
						}

					} catch (NumberFormatException e) {
						e.printStackTrace();
					}

					JOptionPane.showMessageDialog(null, "dark percent must be an integer. percent >= 0", "Error input", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		this.btnDarken.setFont(new Font("Tahoma", Font.BOLD, 13));
		this.btnDarken.setBounds(10, 398, 186, 32);
		this.panel.add(this.btnDarken);
		
		this.btnOpositeDarken = new JButton("Darken Reverse");
		this.btnOpositeDarken.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String ans = JOptionPane.showInputDialog("Enter a dark percent","50");
				if (ans != null) {
					try {
						int percent = Integer.parseInt(ans);
						if (percent > 0) {
							effectPanel.setEffect(new DarkenReverse(getDuration(),percent,panelColor.getBackground()));
							effectPanel.startEffect();
							return;
						}

					} catch (NumberFormatException e2) {
						e2.printStackTrace();
					}

					JOptionPane.showMessageDialog(null, "dark percent must be an integer. percent >= 0", "Error input", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		this.btnOpositeDarken.setFont(new Font("Tahoma", Font.BOLD, 13));
		this.btnOpositeDarken.setBounds(10, 441, 186, 32);
		this.panel.add(this.btnOpositeDarken);

		this.panel_1 = new JPanel();
		this.panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		this.panel_1.setBounds(226, 11, 415, 661);
		this.frmTestProgram.getContentPane().add(this.panel_1);
		this.panel_1.setLayout(null);

		this.effectPanel = new KEffectPanel();
		this.effectPanel.setBounds(1, 1, 413, 659);
		this.panel_1.add(this.effectPanel);
		this.effectPanel.setLayout(null);

		this.lblHelloWorld = new JLabel("HELLO WORLD!");
		this.lblHelloWorld.setHorizontalAlignment(SwingConstants.CENTER);
		this.lblHelloWorld.setFont(new Font("Comic Sans MS", Font.BOLD, 26));
		this.lblHelloWorld.setBounds(10, 11, 393, 53);
		this.effectPanel.add(this.lblHelloWorld);

		this.lblThisIsA = new JLabel("Hello, this program is to test the graphic effects package.");
		this.lblThisIsA.setFont(new Font("Tahoma", Font.PLAIN, 14));
		this.lblThisIsA.setBounds(10, 75, 393, 30);
		this.effectPanel.add(this.lblThisIsA);

		this.textField = new JTextField();
		this.textField.setBounds(10, 179, 115, 20);
		this.effectPanel.add(this.textField);
		this.textField.setColumns(10);

		this.lblEnterYourName = new JLabel("Enter your name");
		this.lblEnterYourName.setBounds(10, 154, 115, 14);
		this.effectPanel.add(this.lblEnterYourName);

		this.textField_1 = new JTextField();
		this.textField_1.setBounds(135, 179, 115, 20);
		this.effectPanel.add(this.textField_1);
		this.textField_1.setColumns(10);

		this.lblEnterAMessage = new JLabel("Enter a message");
		this.lblEnterAMessage.setBounds(135, 154, 115, 14);
		this.effectPanel.add(this.lblEnterAMessage);

		this.list = new JList();
		this.list.setBorder(new LineBorder(new Color(0, 0, 0)));
		this.list.setModel(new AbstractListModel() {

			String[] values = new String[] { "Item1", "Item2", "Item3", "Item4" };


			public int getSize() {
				return values.length;
			}


			public Object getElementAt(final int index) {
				return values[index];
			}
		});
		this.list.setBounds(10, 210, 115, 76);
		this.effectPanel.add(this.list);

		this.table = new JTable();
		this.table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		this.table.setCellSelectionEnabled(true);
		this.table.setFillsViewportHeight(true);
		this.table.setColumnSelectionAllowed(true);
		this.table.setBorder(new LineBorder(new Color(0, 0, 0)));
		this.table.setModel(new DefaultTableModel(new Object[][] { { "a1", "a2", "a3", "a4" }, { "b1", "b2", "b3", "b4" }, { "c1", "c2", "c3", null }, },
				new String[] { "Column4", "Column3", "Column2", "Column1" }));
		this.table.setBounds(145, 210, 258, 76);
		this.effectPanel.add(this.table);
		
		this.lblMatiasVarela = new JLabel("Matias Varela");
		this.lblMatiasVarela.setHorizontalAlignment(SwingConstants.CENTER);
		this.lblMatiasVarela.setFont(new Font("Comic Sans MS", Font.BOLD, 26));
		this.lblMatiasVarela.setBounds(10, 360, 393, 53);
		this.effectPanel.add(this.lblMatiasVarela);
		
		this.lblMtsvrlgmailcom = new JLabel("mtsvrl@gmail.com");
		this.lblMtsvrlgmailcom.setHorizontalAlignment(SwingConstants.CENTER);
		this.lblMtsvrlgmailcom.setFont(new Font("Comic Sans MS", Font.BOLD, 26));
		this.lblMtsvrlgmailcom.setBounds(10, 424, 393, 53);
		this.effectPanel.add(this.lblMtsvrlgmailcom);
		
		this.label = new JLabel("2013");
		this.label.setHorizontalAlignment(SwingConstants.CENTER);
		this.label.setFont(new Font("Comic Sans MS", Font.BOLD, 26));
		this.label.setBounds(10, 595, 393, 53);
		this.effectPanel.add(this.label);
		
		this.panel_2 = new JPanel();
		this.panel_2.setLayout(null);
		this.panel_2.setBorder(new LineBorder(new Color(0, 0, 0)));
		this.panel_2.setBounds(651, 11, 206, 661);
		this.frmTestProgram.getContentPane().add(this.panel_2);
		
		this.panel_3 = new JPanel();
		this.panel_3.setBorder(new LineBorder(new Color(0, 0, 0), 2));
		this.panel_3.setBounds(10, 11, 186, 131);
		this.panel_2.add(this.panel_3);
		this.panel_3.setLayout(null);
		
		this.btnBackgroundImage = new JButton("Background Image");
		this.btnBackgroundImage.setBounds(10, 11, 166, 32);
		this.panel_3.add(this.btnBackgroundImage);
		this.btnBackgroundImage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String path = JOptionPane.showInputDialog("Image path:");
				if (path != null){
					try {
						BufferedImage im= ImageIO.read(new File(path));
						String selection=buttonGroup.getSelection().getActionCommand();
						int type=-1;
						if (selection.equals("NORMAL")) type= BackgroundImage.NORMAL;
						else if (selection.equals("STRETCH")) type= BackgroundImage.STRETCH;
						else if (selection.equals("FIT")) type= BackgroundImage.FIT;
						else if (selection.equals("TILE")) type= BackgroundImage.TILE;
						else{
							JOptionPane.showMessageDialog(null, "An option must be selected", "Error", JOptionPane.ERROR_MESSAGE);
							return;
						}
						effectPanel.setEffect(new BackgroundImage(im,type));
						effectPanel.startEffect();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						JOptionPane.showMessageDialog(null, "Image path incorrect.", "Error input", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
		});
		this.btnBackgroundImage.setFont(new Font("Tahoma", Font.BOLD, 13));
		
		this.rdbtnNormal = new JRadioButton("Normal");
		this.rdbtnNormal.setSelected(true);
		this.rdbtnNormal.setActionCommand("NORMAL");
		buttonGroup.add(this.rdbtnNormal);
		this.rdbtnNormal.setBounds(10, 51, 76, 23);
		this.panel_3.add(this.rdbtnNormal);
		
		this.rdbtnStretch = new JRadioButton("Stretch");
		this.rdbtnStretch.setActionCommand("STRETCH");
		buttonGroup.add(this.rdbtnStretch);
		this.rdbtnStretch.setBounds(10, 77, 76, 23);
		this.panel_3.add(this.rdbtnStretch);
		
		this.rdbtnFit = new JRadioButton("Fit");
		this.rdbtnFit.setActionCommand("FIT");
		buttonGroup.add(this.rdbtnFit);
		this.rdbtnFit.setBounds(102, 51, 67, 23);
		this.panel_3.add(this.rdbtnFit);
		
		this.rdbtnTile = new JRadioButton("Tile");
		this.rdbtnTile.setActionCommand("TILE");
		buttonGroup.add(this.rdbtnTile);
		this.rdbtnTile.setBounds(102, 77, 67, 23);
		this.panel_3.add(this.rdbtnTile);
		
		this.btnMultipleEffects = new JButton("Multiple Effects");
		this.btnMultipleEffects.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				MultipleEffect effects = new MultipleEffect();
				effects.addEffect(new ShadowFrame(20));
				effects.addEffect(new Shadow(1000));
				effects.addEffect(new SplitHorizontalFade(1000));
				effects.addEffect(new SplitVerticalFade(2000));
				effects.addEffect(new DarkenReverse(6000,50));
				
				
				effectPanel.setEffect(effects);
				effectPanel.startEffect();
			}
		});
		this.btnMultipleEffects.setFont(new Font("Tahoma", Font.BOLD, 13));
		this.btnMultipleEffects.setBounds(10, 153, 186, 32);
		this.panel_2.add(this.btnMultipleEffects);
		
		this.btnEfecto = new JButton("Entry Boucing");
		this.btnEfecto.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				effectPanel.setEffect(new EntryBoucing(getDuration()));
				effectPanel.startEffect();
			}
		});
		this.btnEfecto.setFont(new Font("Tahoma", Font.BOLD, 13));
		this.btnEfecto.setBounds(10, 196, 186, 32);
		this.panel_2.add(this.btnEfecto);
		
		this.btnEfecto_1 = new JButton("Shape");
		this.btnEfecto_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				effectPanel.setEffect(new Shape(getDuration()));
				effectPanel.startEffect();
			}
		});
		this.btnEfecto_1.setFont(new Font("Tahoma", Font.BOLD, 13));
		this.btnEfecto_1.setBounds(10, 239, 186, 32);
		this.panel_2.add(this.btnEfecto_1);
		
		this.btnEfecto_2 = new JButton("Zoom In");
		this.btnEfecto_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				effectPanel.setEffect(new ZoomIn(getDuration()));
				effectPanel.startEffect();
			}
		});
		this.btnEfecto_2.setFont(new Font("Tahoma", Font.BOLD, 13));
		this.btnEfecto_2.setBounds(10, 282, 186, 32);
		this.panel_2.add(this.btnEfecto_2);
	}


	public long getDuration() {
		try {
			long duration = Long.parseLong(txtDuration.getText());
			if (duration > 0) return duration;
			else return 1000;
		} catch (NumberFormatException e) {
			JOptionPane.showMessageDialog(null, "The duration time must be long type.", "Error input", JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
			return 1000;
		}
	}
}
